package model;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * data model. 数据库字段名建议使用驼峰命名规则，便于与 java 代码保持一致，如字段名： userId
 */
@SuppressWarnings("serial")
public class User extends Model<User> {
    public static final User me = new User();

    /**
     * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
     */
    public Page<User> paginate(int pageNumber, int pageSize) {
        return paginate(pageNumber, pageSize, "select *", "from user");
    }

    public List<User> getByParams(String[] keys, Object[] values) {
        String keyStr = " where 1=1 ";
        for (String k : keys) {
            keyStr = keyStr + " and " + k + "=? ";
        }
        return me.find("select * from user " + keyStr, values);
    }

    public User getDataByCustomerId(String customerId) {
        List<User> datasByCustomerId = getDatasByCustomerId(customerId);
        if (datasByCustomerId.size() > 0) return datasByCustomerId.get(0);
        return null;
    }

    public List<User> getDatasByCustomerId(String customerId) {
        return getByParams(new String[]{"customer_id"}, new Object[]{customerId});
    }
}
